package cn.youqu.happy.module.youqu.service.userInfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.youqu.happy.module.youqu.controller.admin.userInfo.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userInfo.UserInfoDO;
import cn.youqu.happy.framework.common.pojo.PageResult;

import cn.youqu.happy.module.youqu.convert.userInfo.UserInfoConvert;
import cn.youqu.happy.module.youqu.dal.mysql.userInfo.UserInfoMapper;

import static cn.youqu.happy.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.youqu.happy.module.youqu.enums.ErrorCodeConstants.*;

/**
 * 用户信息 Service 实现类
 *
 * @author 王其岩 20112001045
 */
@Service
@Validated
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public Integer createUserInfo(UserInfoCreateReqVO createReqVO) {
        // 插入
        UserInfoDO userInfo = UserInfoConvert.INSTANCE.convert(createReqVO);
        userInfoMapper.insert(userInfo);
        // 返回
        return userInfo.getId();
    }

    @Override
    public void updateUserInfo(UserInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserInfoExists(updateReqVO.getId());
        // 更新
        UserInfoDO updateObj = UserInfoConvert.INSTANCE.convert(updateReqVO);
        userInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserInfo(Integer id) {
        // 校验存在
        this.validateUserInfoExists(id);
        // 删除
        userInfoMapper.deleteById(id);
    }

    private void validateUserInfoExists(Integer id) {
        if (userInfoMapper.selectById(id) == null) {
            throw exception(USER_INFO_NOT_EXISTS);
        }
    }

    @Override
    public UserInfoDO getUserInfo(Integer id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public List<UserInfoDO> getUserInfoList(Collection<Integer> ids) {
        return userInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserInfoDO> getUserInfoPage(UserInfoPageReqVO pageReqVO) {
        return userInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserInfoDO> getUserInfoList(UserInfoExportReqVO exportReqVO) {
        return userInfoMapper.selectList(exportReqVO);
    }

}
